package com.cmcc.volte.xdr.XDRUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cmcc.volte.xdr.sequence.Arrow;

/**
 * \
 * 
 * @author chenxin
 * @version 1.0 2015-05-14
 */
public class SequenceUtil {

	public static String pcap2svg(List<String> nodeList, List<Arrow> arrowList) {
		StringBuffer sequenceStr = new StringBuffer();
		for (String nodeStr : nodeList) {
			sequenceStr.append("participant " + nodeStr + "\n");
		}
		for (Arrow arrow : arrowList) {
			//对只有哦数字的remark加消息类型
			if(remarkUtil(arrow)!=null){
				arrow.setRemark(remarkUtil(arrow));
			} 
			 
			switch (arrow.getType()) {
			case 0: {
				// 普通箭头  加 中文冒号 
				sequenceStr.append(arrow.getSrcNode() + "->" + arrow.getDestNode() + "：" + arrow.getRemark() + "\n");
				break;
			}
			case 1: {
				// 左边注释
				sequenceStr.append("Note left of " + arrow.getSrcNode() + ":" + arrow.getRemark() + "\n");
				break;
			}
			case 2: {
				// 右边注释
				sequenceStr.append("Note right of " + arrow.getSrcNode() + ":" + arrow.getRemark() + "\n");
				break;
			}
			case 3: {
				// 点到点注释
				sequenceStr.append("Note over " + arrow.getSrcNode() + "," + arrow.getDestNode() + ":" + arrow.getRemark() + "\n");
				break;
			}
			default: {
				sequenceStr.append(arrow.getSrcNode() + "->" + arrow.getDestNode() + ":" + arrow.getRemark() + "\n");
				break;
			}
			}
		}
		return sequenceStr.toString();
	}
	/**
	 * //对只有哦数字的remark加消息类型，重写remark值
	 * @param arrow
	 * @return
	 */
	public static String remarkUtil(Arrow arrow) {
		String methodName = null;
		if(!arrow.getRemark().split("@")[0].matches(".*[a-zA-z].*")){
			String[] ts = arrow.getComment().split("<br>");
			for (int j = 0; j < ts.length; j++) {
				String method = ts[j];
				if (method.contains("CSeq:")) {
					methodName = method.split(" ")[2];
				}
			}
			return  arrow.getRemark().split("@")[0].split("\\.")[0]+"."+methodName+" "+arrow.getRemark().split("@")[0].split("\\.")[1]+"@"+arrow.getRemark().split("@")[1]+"@"+arrow.getRemark().split("@")[2];
		} 
	return null;
	}
	
	//判断sdp 是offer/anwser
	public static Map<String,String> sdpJudge(Map<String, String> mapping){
		int rownum=0;
		//默认为请求
		boolean isReq = false;
		boolean isResp = false;
		Map<String,String> map = new HashMap<String, String>();
		
		 for (Entry<String, String> entry : mapping.entrySet()) {
			 if(entry.getValue().contains("Content-Type: application/sdp")){
					rownum++;
					// 通过找 方括号中是否包含 数字 来判断为请求还是响应 ; "3.【INVITE 100】@|2017-07-18 16:39:00:904@|
					if(rownum==1){
						map.put("offer", entry.getValue());
						String key = entry.getKey();
						isReq = !hasDigit(key);
						if(isReq){
							map.put("offerType", "isReq");
							map.put("answerType","isResp");
						}else{
							map.put("offerType", "isResp");
							map.put("answerType","isReq");
						}
					}else{
						String key = entry.getKey();
						//与第一个sdp消息相反
						isResp = !hasDigit(key);
						if(isReq!=isResp){
							map.put("answer", entry.getValue());
							break;
						}
					}
					
				}
		 
		 }
		return map;
	}
	
	
	// 判断一个字符串是否含有数字
	public static boolean hasDigit(String key) {
	    String temp = key.split("@\\|")[0];
	    String content = temp.split("\\.")[1];
		
	    boolean flag = false;
	    Pattern p = Pattern.compile(".*\\d+.*");
	    Matcher m = p.matcher(content);
	    if (m.matches()) {
	        flag = true;
	    }
	    return flag;
	}

}